var apiHost = wx.getStorageSync("apiHost");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    state :'1',       //订单状态
    orderList: [],    //订单集合
    orderListFlag:[], //分页订单
    orderListLength: '10',//拼团集合长度
    orderListStart: '0',  //拼团集合开始
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userId = wx.getStorageSync("userId");     //用户id
    var start = that.data.orderListStart;
    var length = that.data.orderListLength;

    var state = that.data.state;     //用户id
    that.initHearOrderList(userId, state, start, length);
    that.setData({
      userId: userId,
      state: state
    });
  },

  /**
   * 订单筛选
   */
  orderScreen : function(e){
    var that = this;
    var state = e.currentTarget.dataset.state;  //点击订单状态状态
    var userId = that.data.userId;
    that.initHearOrderList(userId, state,"0","10");
    that.setData({
      state: state
    })
  },

  /**
   * 初始化 团长订单管理
   */
  initHearOrderList: function (userId, state,start,length){
    var that= this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
    wx.request({
      url: apiHost + 'hpp/order/myCollageOrderList',
      method:'get',
      header : {
        'content-type' :'application/json'
      },
      data : {
        userId: userId,
        state: state,
        start: start,
        length: length
      },
      success : function(res){
        console.log(res)
        var code = res.data.code;
        var msg = res.data.msg;
        if ("10000" == code){
          var orderList = res.data.data;
          that.setData({
            orderList: orderList
          });
        }else{
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: false
          })
        }
        wx.hideLoading();
      },
      fail : function(){
        wx.hideLoading();
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: false
        })
      }
    })
  },

  /**
   * 分页订单
   */
  initHearOrderListOrder: function (userId, state, start, length) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
    wx.request({
      url: apiHost + 'hpp/order/myCollageOrderList',
      method: 'get',
      header: {
        'content-type': 'application/json'
      },
      data: {
        userId: userId,
        state: state,
        start: start,
        length: length
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        if ("10000" == code) {
          var orderList = res.data.data;
          var orderListFlag = that.data.orderList;

          // 追加数据
          for (var i = 0; i < orderList.length; i++) {
            orderListFlag.push(orderList[i])
          }

          that.setData({
            orderList: orderListFlag
          });
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000,
            mask: false
          })
        }
        wx.hideLoading();
      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: '网络异常',
          icon: 'none',
          duration: 2000,
          mask: false
        })
      }
    })
  },

  /**
   * 立即发货
   */
  nowDeliver : function(e){
    var that = this;
    var orderNo = e.currentTarget.dataset.orderno
    wx.showActionSheet({
      itemList: ['快递配送', '自己配送'],
      success: function (res) {
        var index = res.tapIndex;
        if (index==0){
          that.goWxml("../expressDelivery/expressDelivery?orderNo=" + orderNo+"");
        }
        if (index == 1) {
          that.goWxml("../ownDelivery/ownDelivery?orderNo=" + orderNo + "");
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  /**
   * 跳转订单详情
   */
  goOrderDetail : function(e){
    var that = this;
    var orderNo =  e.currentTarget.dataset.orderno;
    var flaghear = e.currentTarget.dataset.flaghear;
    that.goWxml("/pages/orders/orderDetail/orderDetail?orderNo=" + orderNo + "&flaghear=" + flaghear+"");
  },
  /**
  * 跳转页面
  */
  goWxml: function (url) {
    var that = this;
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });

    wx.navigateTo({
      url: url,
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var start = that.data.orderListStart;
    var length = that.data.orderListLength;
    var userId = that.data.userId;
    var state = that.data.state;
    var startFlag = parseInt(start) + 1

    that.setData({
      orderListStart: startFlag
    });

    that.initHearOrderListOrder(userId, state, startFlag, length) 
  }
})